<?php

namespace common\models\cms;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%cms_channel}}".
 *
 * @property int $id
 * @property string $name 名称
 * @property string $abbr 缩写
 * @property string $icon 图标
 * @property string $cover 封面
 * @property string $template 自定义显示模版
 * @property string $link 跳转链接
 * @property int $listorder 排序
 * @property int $recommend_at 推荐时间
 * @property int $status 状态
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class CmsChannel extends \common\db\ActiveRecord {

    /**
     * 频道状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_channel}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name', 'abbr'], 'required'],
            [['listorder', 'recommend_at', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name', 'abbr'], 'string', 'max' => 50],
            [['icon', 'cover', 'link'], 'string', 'max' => 255],
            [['template'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '名称',
            'abbr' => '缩写',
            'icon' => '图标',
            'cover' => '封面',
            'template' => '自定义显示模版',
            'link' => '跳转链接',
            'listorder' => '排序',
            'recommend_at' => '推荐时间',
            'status' => '状态',
            'created_at' => 'Created At',
            'updated_at' => '更新时间',
            'deleted_at' => 'Deleted At',
        ];
    }

    /**
     * 特殊栏目显示时
     */
    public function getWebLink() {
//        if ($this->link) {
//            return $this->link;
//        }
//        if ($this->template) {
//            return Url::h5('article/channel/' . $this->template);
//        }
        return Url::h5('article/channel/' . $this->id);
    }

    /**
     * 频道图标
     */
    public function getThumb() {
        return 'http://img.lnts.cn/attachments/fanasoc/default.png';
//        return Url::attach('assets/icons/category/' . $this->id . '.png');
    }

    /**
     * 频道信息基础数据结构
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'name',
            'thumb',
            'link' => 'webLink',
        ];
    }

}
